#pragma once
 #include <arpa/inet.h>
#include<netinet/in.h>
#include<string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include"Common.hpp"

class InetAddr
{
private:
    void PortNetToHost()
    {
        _port=::ntohs(_net_addr.sin_port);
    }
    void IpNetToHost()
    {
        char ipbuffer[64];
        _ip=::inet_ntop(AF_INET,&_net_addr.sin_addr,ipbuffer,sizeof(ipbuffer));
    }
public:
    InetAddr(const struct sockaddr_in& addr)
    :_net_addr(addr)
    {
        PortNetToHost();
        IpNetToHost();
    }
    
    InetAddr(uint16_t port)
    :_port(port)
    ,_ip("")
    {
        _net_addr.sin_family=AF_INET;
        _net_addr.sin_port=::htons(_port);
        _net_addr.sin_addr.s_addr=INADDR_ANY;//该宏值为0
    }

    struct sockaddr* NetAddr(){return CONV(&_net_addr);}
    socklen_t NetAddrLen(){return sizeof(_net_addr);}//返回类型加头文件
    std::string Ip() {return _ip; }
    uint16_t Port() { return _port; }

private:
    struct sockaddr_in _net_addr;//网络字节序和整型表示

    std::string _ip;//主机字节序和点分十进制表示
    uint16_t _port;

};